package joyi.train.service.impl;

import j.data.tree.TreeData;
import j.data.tree.TreeNode;
import j.data.tree.TreeSetting;
import j.framework.core.ServiceContext;
import j.framework.core.ServiceException;
import j.util.New;
import j.util.StringUtils;
import j.util.jdbc.JdbcTemplate;
import j.util.jdbc.RowMapper;
import j.util.pkgenerator.PG;
import joyi.train.domain.CustomerCategory;
import joyi.train.service.UserManagerService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 注释模板
 */
public class UserManagerServiceImpl implements UserManagerService {
    @Override
    public Map<String,Object> getUserCategoryTreeGrid(ServiceContext serviceContext) {
        Map<String,Object> data = New.map();
        JdbcTemplate jt = serviceContext.getJdbcTemplate();
        List<Map<String,Object>> listData = jt.queryForListMap("select custCategoryId as id,categoryName as name,categoryParentId as _parentId from cust_CustomerCategory");
        data.put("total",listData.size());
        data.put("rows",listData);
        return data;
    }

    @Override
    public TreeData getUserCategoryTree(ServiceContext serviceContext) {
        JdbcTemplate jt = serviceContext.getJdbcTemplate();
        List<CustomerCategory> list = jt.queryForList("select * from cust_CustomerCategory",
                customerCategoryRowMapper);
        TreeSetting setting = new TreeSetting(false,false);
        setting.setNameFieldName("text");
        TreeData td = new TreeData(setting);
        TreeNode node;
        for (CustomerCategory cc : list){
            node = new TreeNode(cc.getId(),cc.getCategoryName());
            node.setpId(cc.getParentCategoryId());
            td.addNode(node);
        }

        return td;
    }

    @Override
    public void saveUserCategory(ServiceContext serviceContext) {
        /*String name = serviceContext.params.getString("categoryName");
        String pid = serviceContext.params.getString("parentCategoryId");
        String note = serviceContext.params.getString("note");
        CustomerCategory cc = new CustomerCategory();
        cc.setCategoryId(PG.getNext("UCT"));
        cc.setCategoryName(name);
        cc.setParentCategoryId(pid);
        cc.setNote(note);
        cc.setAddUserId(serviceContext.getUser().getUserId());
        cc.setAddDateTime(new Date());*/
        CustomerCategory cc = serviceContext.getBean(CustomerCategory.class);
        if(StringUtils.isNullOrEmpty(cc.getId())){
            //这是增加
            cc.setCategoryId(PG.getNext("UCT"));
            cc.setAddedUserId(serviceContext.getUser().getUserId());
            cc.setAddedTime(new Date());
            serviceContext.getJdbcTemplate().insert(cc);
        }else{
            serviceContext.getJdbcTemplate().update(cc);
        }
    }

    /**
     * 删除用户分类，如果分类下有子分类，提示且不允许删除；
     * 如果分类下有客户，提示且不允许删除
     * @param serviceContext
     */
    @Override
    public Map<String,Object> deleteUserCategory(ServiceContext serviceContext) {
        String id=serviceContext.params.getString("id");
        JdbcTemplate jt = serviceContext.getJdbcTemplate();
        //判断分类下是否有客户
        String sql_cust="select count(*) from cust_Customer where custCategoryId=?";
        //判断分类下是否有子分类
        String sql_cate="select count(*) from cust_CustomerCategory where categoryParentId=?";
        //删除客户
        String sql="delete from cust_CustomerCategory where custCategoryId=?";

        int cnt=jt.queryForInt(sql_cate,id);
        if(cnt > 0){
            //serviceContext.actionResult.add("err", "有子分类");
            throw new ServiceException("[异常方式]有子分类！");
        }
        //客户数
        cnt=jt.queryForInt(sql_cust,id);
        if(cnt > 0){
            serviceContext.actionResult.add("err", "有客户");
            return null;
        }
        //删除
        //1.用实体类删除
       /* CustomerCategory cc = new CustomerCategory();
        cc.setCategoryId(id);
        jt.delete(cc);
        jt.update(cc);
        jt.insert(cc);*/

        //2.用sql语句删除
        //select 查询 jt.query*
        //update insert delete操作语句 jt.execute jt.executeBatch
        jt.execute("delete from cust_CustomerCategory where custCategoryId=?",
                id);
        Map<String,Object> map = New.map();
        map.put("categoryId",id+"abc");
        map.put("c_name","中文");
        return map;
    }

    @Override
    public CustomerCategory doShowEditCustomerCategoryUI(ServiceContext serviceContext) {
        String id = serviceContext.params.getString("id");
        if(StringUtils.isNullOrEmpty(id)){
            return null;
        }
        //1.以jdbc 获取实体类
        CustomerCategory cc;
        /*cc = serviceContext.getJdbcTemplate().queryForDomain("select * from cust_CustomerCategory where custCategoryId=?",
                customerCategoryRowMapper,id);*/

        //2.以实体类形式获取
        Map<String,Object> data = New.map();
        data.put("categoryId",id);
        cc = serviceContext.getJdbcTemplate().findByPk(CustomerCategory.class,data);
        return cc;
    }

    public static RowMapper<CustomerCategory> customerCategoryRowMapper = new RowMapper<CustomerCategory>() {
        @Override
        public CustomerCategory mapRow(ResultSet rs, int rowNum) throws SQLException {
            CustomerCategory cc = new CustomerCategory();
            cc.setCategoryId(rs.getString("custCategoryId"));
            cc.setCategoryName(rs.getString("categoryName"));
            cc.setParentCategoryId(rs.getString("categoryParentId"));
            cc.setNote(rs.getString("note"));
            return cc;
        }
    };
}
